package Options;

import java.io.File;
import java.nio.file.InvalidPathException;
import java.util.LinkedList;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import Main.main;
import Sources.SourcesAdapter;
import System.Classifier;
import System.Listener;
import com.cloudgarden.resource.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Text;
/*This class Wraps the options class adding the GUI*/

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
* @author Shinichi
* GUI for Options 
*/
public class guiOptions extends Options {
		//private boolean on=false;
        public SourcesAdapter mySources;
        private Label label1;
        private Text text2;
        private Text text1;
        public Listener myListener;//a pointer to Listener 
        private Button button = null;
        private Label Name;
        private Button button1;
        private Text input;
        private List SList;
        private Button button2 = null;
        private Button button3 = null;
        
        public guiOptions(SourcesAdapter source, Listener yourListener) {
                super(source, yourListener);
        }
        private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="10,10"

        /**
         * This method initializes sShell
         */
        private void createSShell() {
                sShell = new Shell(Display.getDefault(),0);

                {
                        //Register as a resource user - SWTResourceManager will
                        //handle the obtaining and disposing of resources
                        SWTResourceManager.registerResourceUser(sShell);
                }
                File f = new File("");
                String go=new String(f.getAbsolutePath());
                Image bg=new Image(Display.getDefault(),go+"/1488184-xs.jpg");
                sShell.setText("Options Menu");
                //sShell.setSize(313, 204);
                sShell.setBounds(627, 317, 313, 204);
                sShell.setLayout(null);
                sShell.setBackgroundImage(bg);
                button = new Button(sShell, SWT.NONE);
                button.setBounds(160, 155, 71, 23);
                button.setText("Change");
                button.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
                button.setBackgroundImage(bg);
                button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
                        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                                if(input.getText()==""){
                                        MessageBox messageBox = 
                                new MessageBox(sShell, SWT.OK);
                                        messageBox.setText("Dude...");
                                messageBox.setMessage("Write the new name above the add button\n"+
                                                                          "If you wish to delete the source press Delete\n");
                                messageBox.open();      
                                }
                                else{
                                        String to=new String(input.getText());
                                        String from=new String(SList.getSelection()[0]);
                                        String[] exp=text2.getText().split(",");
                                        setSource(from, to,exp);
                                        main.menu.log.update("Changed source from: "+from+" to: "+to);
                                        getSources();
                                        SList.removeAll();
                                        for(int i=0;i<sList.size();i++){
                                                SList.add(sList.get(i));
                                        }
                                }
                        }
                });
                button2 = new Button(sShell, SWT.NONE);
                button2.setBounds(45, 32, 71, 23);
                button2.setText("Add");
                button2.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
                button2.setBackgroundImage(bg);
                button2.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent evt) {
                        	String str=new String(input.getText());
                        	if(str.lastIndexOf("\\")!=(str.length()-1)){
    	    					MessageBox messageBox = new MessageBox(sShell, SWT.OK);
        	    				messageBox.setText("Error!!");
        		    			messageBox.setMessage("Missing \\ at the end of the statement");
        		    			messageBox.open();	
        		    			return;
                        	}
                        	try{
                                String[] exp=null;
                                if(text2.getText()==""){
                                	exp=new String("*").split(",");
                                }
                                else{
                                	exp=text2.getText().split(",");
                                }
                                addSource(str,exp);
                                main.menu.log.update("Added source: "+str);
                                getSources();
                                SList.removeAll();
                                for(int i=0;i<sList.size();i++){
                                        SList.add(sList.get(i));
                                }
                        	}catch(InvalidPathException e){
    	    					MessageBox messageBox = 
    		    				new MessageBox(sShell, SWT.OK);
    	    					messageBox.setText("Error!!");
    		    				messageBox.setMessage("Invalid path entered or the path doesn't exist.\n"+
    		    									  "The correct syntax is [local disk name]:\\[folder name]\\*\n"+
    		    									  "Where:\n\t[local disk name]= C or D ect..\n"+
    		    									  "\t[folder name] =name of a folder\n"+
    		    									  "\t\\ separates between folders\n");
    		    				messageBox.open();
                        	}
                        	input.setText("");
                        	text2.setText("");
                        }
                });
                button3 = new Button(sShell, SWT.NONE);
                button3.setBounds(230, 155, 71, 23);
                button3.setText("Delete");
                button3.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
                button3.setBackgroundImage(bg);
                {
                        SList = new List(sShell, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
                        SList.setBounds(164, 60, 134, 90);
                        //SList.setBackgroundImage(bg);
                        for(int i=0;i<sList.size();i++){
                                SList.add(sList.get(i));
                        }
                }
                {
                        Name = new Label(sShell, SWT.SHADOW_OUT | SWT.CENTER | SWT.WRAP | SWT.BORDER);
                        Name.setText("Listening to:");
                        Name.setBounds(164, 32, 134, 19);
                        //Name.setBackgroundImage(bg);
                }
                {
                        input = new Text(sShell, SWT.BORDER);
                        input.setBounds(14, 6, 134, 20);
                        //input.setBackgroundImage(bg);
                }
                final Combo comboAlgo = new Combo(sShell, SWT.NONE);
                Button btnclassifier = new Button(sShell, SWT.NONE);
                btnclassifier.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent arg0) {
                                Classifier clasi=new Classifier();
                                try {
                					if(comboAlgo.getSelectionIndex()>-1){
                						text1.setText(clasi.setAlgorithm(comboAlgo.getItem(comboAlgo.getSelectionIndex())));
                						MessageBox messageBox = new MessageBox(sShell, SWT.YES|SWT.NO);
                						messageBox.setMessage("Do you wish to add this algorithm?");
                						if(messageBox.open()==SWT.YES){
                							MessageBox messageBox2 =new MessageBox(sShell, SWT.OK);
                							messageBox2.setMessage("Processing your request...");
                							messageBox2.open();
                						}
                						else{
                							MessageBox messageBox2 =new MessageBox(sShell, SWT.OK);
                							messageBox2.setMessage("As you wish...");
                							messageBox2.open();
                							text1.setText("");
                							return;
                						}
                					}
                					//main.listener.myClassifiers.add(clasi);
                					String desc=clasi.getAlgorithmInts().getDescription();
                					LinkedList<Classifier> list=main.listener.getClassifiers();
                					for(int i=0;i<list.size();i++){
                						if(list.get(i).getAlgorithmInts().getDescription().equals(desc)){
                							MessageBox messageBox = new MessageBox(new Shell(new Display()), SWT.OK);
                							messageBox.setText("Error!!");
                							messageBox.setMessage("The selected algorithm already in use\n");
                							messageBox.open();
                							text1.setText("");
                    		    		return;
                						}
                					}
                					list.add(clasi);
        	    					MessageBox messageBox = new MessageBox(sShell, SWT.OK);
            	    				messageBox.setText("Done.");
            		    			messageBox.setMessage("The selected algorithm was added\n");
            		    			messageBox.open();
            		    			text1.setText("");
                                } catch (Exception e) {}
                        }
                });
                btnclassifier.setBounds(7, 86, 71, 23);
                btnclassifier.setText("Show");
                btnclassifier.setBackgroundImage(bg);
                {
                	button1 = new Button(sShell, SWT.PUSH | SWT.CENTER);
                	button1.setText("Remove");
                	button1.setBounds(81, 86, 71, 23);
                	button1.setBackgroundImage(bg);
                	button1.addSelectionListener(new SelectionAdapter() {
                		public void widgetSelected(SelectionEvent evt) {
                			//get name of algo to remove
                			try {
                				String name=comboAlgo.getItem(comboAlgo.getSelectionIndex());
								String desc=new Classifier().setAlgorithm(name);
								LinkedList<Classifier> list=main.listener.getClassifiers();
								for(int i=0;i<list.size();i++){
									if(list.get(i).getAlgorithmInts().getDescription().equals(desc)){
										list.remove(i);
		    	    					MessageBox messageBox = new MessageBox(sShell, SWT.OK);
		        	    				messageBox.setText("Done.");
		        		    			messageBox.setMessage("The selected algorithm has been successfully removed\n");
		        		    			messageBox.open();
		        		    			return;
									}
								}
    	    					MessageBox messageBox = new MessageBox(sShell, SWT.OK);
        	    				messageBox.setText("Error!!");
        		    			messageBox.setMessage("The selected algorithm isn't currently used\n");
        		    			messageBox.open();
								
							} catch (Exception e) {}
                		}
                	});
                }
                {
                	text1 = new Text(sShell, SWT.MULTI | SWT.WRAP|SWT.BORDER|SWT.V_SCROLL);
                	text1.setBounds(14, 115, 134, 70);
                }
                {
                	label1 = new Label(sShell, SWT.BORDER);
                	label1.setText("Reg:");
                	label1.setBounds(164, 6, 24, 20);
                }
                {
                	text2 = new Text(sShell, SWT.BORDER);
                	text2.setBounds(193, 6, 105, 20);
                }
                comboAlgo.setBounds(14, 60, 134, 20);
                //NEED to change to general!!! Done =)
                File dir=new File(go+"/logics/");
                String[]files=dir.list();
                for(int i=0;i<files.length;i++){
                        if((files[i].compareTo("ClasifyByKeyWords$keyWordsDictionary.class")!=0)&&
                           (files[i].compareTo("logics_default.txt")!=0)&&
                           (files[i].compareTo("ClasifingAlgorithm.class")!=0))
                        {
                                comboAlgo.add(files[i]);
                        }
                }
                button3.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
                        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                                //mySources.deleteSource(SList.getSelection()[0]);
                                int i=SList.getSelectionIndex();
                                if(i>=0){
                                        System.out.println(SList.getSelection()[0]);
                                        String str=SList.getSelection()[0];
                                        deleteSource(str);
                                        getSources();
                                        SList.remove(str);
                                        main.menu.log.update("Deleted source: "+str);
                                }
                        }
                });
                
        }

        //-tells it when the  listening sources have changed
        /**
         * Description of the method on.
         *
         */
        /*
         * c-tor
         */
        
        public void Open(){
            if((sShell==null)||(sShell.isDisposed())){
            	createSShell();
            	sShell.open();
            }
            else{
				//sShell.close();
				sShell.dispose();
			}

        }
        

}
